﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CharToImage
{
    public partial class Form1 : Form
    {
        private string current_dir;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.current_dir = System.Environment.CurrentDirectory;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = this.openFileDialog1.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string imageFolder = this.current_dir + "\\images";
            string inputFile = this.textBox1.Text.Trim();
            int size = 32;
            string extention = ".bmp";
            string selected_size = this.comboBox1.Text.Trim();
            if (selected_size == "48x48") size = 48;
            else if (selected_size == "64x64") size = 64;
            else if (selected_size == "128x128") size = 128;
            else size = 32;
            string selected_ext = this.comboBox2.Text.Trim();
            if (selected_ext == ".png") extention = ".png";
            else if (selected_ext == ".jpg") extention = ".jpg";
            else if (selected_ext == ".gif") extention = ".gif";
            else extention = ".bmp";

            if (!File.Exists(this.textBox1.Text.Trim()))
            {
                MessageBox.Show("请先选择输入文件！");
                return;
            }
            if (Directory.Exists(imageFolder)) Directory.Delete(imageFolder, true);
            Directory.CreateDirectory(imageFolder);

            FileStream fs = new FileStream(inputFile, FileMode.Open);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();

            string txt = Encoding.GetEncoding(936).GetString(data);
            string[] lines = txt.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            string[] splits = {" ","\t"};
            for (int i = 0; i < lines.Length; ++i)
            {
                string line = lines[i];
                string[] cols = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);
                if (cols.Length > 1)
                {
                    this.label5.Invalidate();
                    this.label6.Invalidate();
                    this.label6.Text = cols[0] + extention;
                    this.label5.Text = cols[1];                   
                    this.label5.Update();
                    this.label6.Update();

                    Bitmap bmp = ImageProcess.CreateFromChar_48X48(cols[1]);
                    ImageProcess.SaveToFile(imageFolder + "\\" + cols[0] + extention, size, bmp);
                    bmp.Dispose();
                }
            }
        }
    }
}
